package org.lemon.transport.context.data.queue.message;

import org.lemon.transport.context.data.queue.QueueRequestUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class QueueMessageHeaders {

    protected final Map<String, byte[]> data = new HashMap<>();

    public byte[] put(String key, byte[] value) {
        return data.put(key, value);
    }

    public byte[] get(String key) {
        return data.get(key);
    }

    public Map<String, byte[]> getData() {
        return data;
    }

    public void putRequestId(UUID requestId) {
        data.put(QueueRequestUtil.REQUEST_ID_HEADER, QueueRequestUtil.uuidToBytes(requestId));
    }

    public UUID gutRequestId() {
        byte[] requestId = data.get(QueueRequestUtil.REQUEST_ID_HEADER);
        if (requestId == null) {
            return null;
        }
        return QueueRequestUtil.bytesToUuid(requestId);
    }

    public void putResponseTopic(String topic) {
        data.put(QueueRequestUtil.RESPONSE_TOPIC_HEADER, QueueRequestUtil.stringToBytes(topic));
    }

    public String gutResponseTopic() {
        byte[] topicBytes = data.get(QueueRequestUtil.RESPONSE_TOPIC_HEADER);
        if (topicBytes == null) {
            return null;
        }
        return QueueRequestUtil.bytesToString(topicBytes);
    }

    public void putExpireTs(long timeMs) {
        data.put(QueueRequestUtil.EXPIRE_TS_HEADER, QueueRequestUtil.longToBytes(timeMs));
    }

    public long gutExpireTs() {
        byte[] expireTsBytes = data.get(QueueRequestUtil.EXPIRE_TS_HEADER);
        if (expireTsBytes == null) {
            return 0;
        }
        return QueueRequestUtil.bytesToLong(expireTsBytes);
    }
}
